<?php
class Con_user extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('auth');
		$this->load->model('usermodel');
	}
	public function list_user()
	{
		$this->auth->restrict();
		$data['menu'] = $this->usermodel->get_menu();
		$data['all_user'] = $this->usermodel->get_all_user();
		$this->template->set('title','Daftar User | Aplikasi Persuratan');
		$this->template->load('template','list_user',$data);
	}
	public function tambah()
	{
		$this->auth->restrict();
		$this->load->library('form_validation');
		$this->form_validation->set_rules('nama','nama','trim|required');
		$this->form_validation->set_rules('nip','nip','trim|required');
		$this->form_validation->set_rules('username','username','trim|required');
		$this->form_validation->set_rules('password','password','trim|required');
		$this->form_validation->set_rules('password_conf','password_conf','trim|required|matches[password]');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = $this->usermodel->get_menu();
			$this->template->set('title','Tambah User Baru | Aplikasi Persuratan');
			$this->template->load('template','form_tambah_user',$data);
		}
		else
		{
			$data_user = array (
			'nama' =>$this->input->post('nama'),
			'nip' =>$this->input->post('nip'),
			'username' =>$this->input->post('username'),
			'password' =>md5($this->input->post('password'))
			);
			$this->usermodel->tambah_data_user($data_user);
			//kembali ke halaman manajemen user
			redirect ('con_user/list_user');
		}
	}
	public function edit()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('nama','nama','trim|required');
		$this->form_validation->set_rules('nip','nip','trim|required');
		$this->form_validation->set_rules('username','username','trim|required');
		$this->form_validation->set_rules('password','password','trim|required');
		$this->form_validation->set_rules('password_conf','password_conf','trim|required|matches[password]');
	
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		$id = $this->uri->segment(3);
		
		if ($this->form_validation->run() == FALSE)
		{
			//$data['level'] = $this->usermodel->get_all_level();
			//dapatkan user yang akan diedit
			$data['menu'] = $this->usermodel->get_menu();
			$data['user'] = $this->usermodel->get_user_by_id($id);
			$this->template->set('title','Edit User | Aplikasi Persuratan');
			$this->template->load('template','form_edit_user',$data);
		}
		else
		{
			$data_user = array (
			'nama' =>$this->input->post('nama'),
			'nip' =>$this->input->post('nip'),
			'username' =>$this->input->post('username'),
			'password' =>md5($this->input->post('password'))
			);
			$this->usermodel->edit_data_user($data_user,$id);
			//kembali ke halaman manajemen user
			redirect ('con_user/list_user');
		}
	}
	public function hapus()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		//dapatkan id user dari segment ke-3 dari URI
		$id = $this->uri->segment(3);
		$this->usermodel->delete_user($id);
		//kembali ke halaman manajemen user
		redirect ('con_user/list_user');
	}
		
}